package com.yhm.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.yhm.dao.AllDAO;
import com.yhm.model.Dep;
import com.yhm.model.OrderFrom;
import com.yhm.model.Redo;
import com.yhm.service.IndexManager;

@Component("indexManager")
public class IndexManagerImpl implements IndexManager{

	private AllDAO allDAO = new AllDAO();
	
	private SimpleDateFormat fdate = new SimpleDateFormat("yyyy-MM-dd");
	/**
	 * 所有订单统计
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String order_stat(){
		String json = "";
		List<Dep> dep_list = (List<Dep>) allDAO.getDepDAO().findhql("from Dep d order by d.num ASC");
		json = "{\"total\":0 ," +
				 "\"title\":\"\","+
				 "\"x\":\"部门\","+
				 "\"n\":\"1\","+
				"\"rows\":\"[";
		for(Dep d : dep_list){
			List<?> order_count_list = (List<?>) allDAO.getOrderFromDAO().findhql("select count(*) from OrderFrom o where o.department = '"+d.getDepartment()+"'");
			json = json.concat("['"+d.getDepartment()+"'," + order_count_list.get(0) + "],");
		}
		
		if(dep_list.size() != 0)
			json = json.substring(0, json.length()-1).concat("]\"}");
		else
			json = json.concat("]\"}");
		return json;
	}
	
	/**
	 * 标记订单列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String order_sign(){
		String json = "";
		
		List<?> order_lists = allDAO.getOrderFromDAO().findhql("select COUNT(*) from OrderFrom o where o.qc2 = '0' and o.sign = '1' order by o.makeTime asc");
		int ak = ((Long) order_lists.get(0)).intValue();
		if(ak == 0){
			json = "{\"total\":0,\"rows\":[]}";
			return json;
		}
		List<OrderFrom> order_list = (List<OrderFrom>) allDAO.getOrderFromDAO().findhql("from OrderFrom o where o.qc2 = '0' and o.sign = '1' order by o.makeTime asc");
		
		json = "{\"total\":" + 0 + "," +
				"\"rows\":[";
		for(OrderFrom of : order_list){
			json = json.concat("{\"companyNum\":\"" + of.getCompanyNum() + "\"," +
					"\"dep\":\"" + of.getDepartment() + "\"" +
					"},");
		}
		if(order_list.size() != 0)
			json = json.substring(0, json.length()-1).concat("]}");
		else
			json = json.concat("]}");
		return json;
	}
	
	
	/**
	 * 新订单统计
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String new_order_stat(){
		String json = "";
		Date now = new Date();
		Date old = addDay(now, -6);
		String s_now = fdate.format(now);
		String s_old = fdate.format(old);
		List<List> order_count_list = (List<List>) allDAO.getOrderFromDAO().findhql("select new list(o.makeTime,count(*)) from OrderFrom o where (o.makeTime " + "between '"+s_old+"' and '"+s_now+"') group by o.makeTime");
		json = "{\"total\":0 ," +
				 "\"title\":\"\","+
				 "\"x\":\"部门\","+
				 "\"n\":\"1\","+
				"\"rows\":\"[";
		String com = "\"rows2\":\"[";
		List<String[][]> com_list = new ArrayList<String[][]>();
		for(int a=6;a>=0;a--){
			String[][] count = new String[1][2];
			Date c_date = addDay(now, -a);
			String s_c_old = fdate.format(c_date);
			int sign = 0;
			for(List order_count : order_count_list){
				if(s_c_old.equals(order_count.get(0))){
					count[0][0] = (String) order_count.get(0);
					count[0][1] = String.valueOf(order_count.get(1));
					sign++;
				}
			}
			if(sign==0){
				count[0][0] = s_c_old;
				count[0][1] = "0";
				com_list.add(count);
			}else{
				com_list.add(count);
			}
		}
		
		int i = 1;
		for(String[][] order_count : com_list){
			String[] sc_d = order_count[0][0].split("-");
			String c_sc_d = sc_d[1]+"."+sc_d[2];
			json = json.concat("["+i+",'" +c_sc_d + "'],");
			com = com.concat("["+i+",'" + order_count[0][1] + "'],");
			i++;
        } 
		
		if(order_count_list.size() != 0){
			json = json.substring(0, json.length()-1) + "]\"," + com;
			json = json.substring(0, json.length()-1).concat("]\"}");
		}
		else
			json = json.concat("]\"}");
		System.out.println(json);
		return json;
	}
	
	
	 //实现日期加一天的方法
    public static Date addDay(Date date, int n) {   
        try {   
                 Calendar cd = Calendar.getInstance();   
                 cd.setTime(date);   
                 cd.add(Calendar.DATE, n);//增加一天   
                 return cd.getTime();   
             } catch (Exception e) {   
                 return null;   
             }   
     }  
	
	@Resource(name = "allDAO")
	public void setAllDAO(AllDAO allDAO) {
		this.allDAO = allDAO;
	}
}
